import { ApiCall } from "tsrpc";
import { ReqCreateShow, ResCreateShow } from "../../shared/api_client/mod/PtlCreateShow";
import player from "../../core/rpg/entity/player";
import sdk, { MailManage } from "../../core";
import { ItemType, quality } from "../../shared/face/enums";

export default async function (call: ApiCall<ReqCreateShow, ResCreateShow>) {
    try {
        // 获取当前用户
        const user = call.req._player as player;
        if (!user) {
            call.error('用户未登录');
            return;
        }

        // 参数验证
        if (!call.req.url) {
            call.error('图片URL不能为空');
            return;
        }

        // 验证图片URL格式
        if (!call.req.url.startsWith('http')) {
            call.error('图片URL格式不正确');
            return;
        }

        // TODO 后续接入图像审核API
        // 保存形象数据并直接发送邮件
        const rewardItems = [
            sdk.prop.createProp_customize({
                type: ItemType.消耗品,
                id: sdk.tool.common.uuid,
                tag: '形象卡',
                count: 1,
                uuid: sdk.tool.common.uuid,
                emoji: '🎨',
                icon: '',
                quality: quality.SSR,
                name: '形象卡',
                desc: `使用后可更换形象为<img src="${call.req.url}" />`,
                stacking: false,
                stacking_use: false,
                data: {
                    funcName: 'ChangePlayerShow',
                    funcData: call.req.url,
                },
            }),
        ];

        // 发送邮件和奖励
        await MailManage.send(
            'admin',
            user.id,
            30, // 30天过期
            '形象上传成功',
            `恭喜！您的形象已成功上传。`,
            rewardItems
        );

        
        // 返回成功响应
        call.succ({
            message: '形象上传成功，已自动通过并发送邮件',
        });

    } catch (error) {
        console.error('形象上传失败:', error);
        call.error('形象上传失败，请稍后重试');
    }
}
